package com.TestDemo.cloneTest;

public class StudentDeepClone implements Cloneable{
    private String name;
    private int age;
    private Address addr;
    // 重写克隆方法
    @Override
    public Object clone(){
        StudentDeepClone student = null;
        try{
            student = (StudentDeepClone) super.clone(); //浅复制
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        student.addr = (Address) addr.clone(); // 深度复制
        return student;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddr() {
        return addr;
    }

    public void setAddr(Address addr) {
        this.addr = addr;
    }

}

// 实现深拷贝
class Address implements Cloneable{
    private String address;

    public Object clone(){
        Address address = null;

        try{
            address = (Address) super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }

        return address;
    }



    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}